/**
 * @author hoang.pt
 * @version $Id: StudentManager.java 247 2010-09-15 12:46:56Z phantichhoang $
 */

package test4.problem1;

import java.io.*;
import java.util.*;

/**
 * Student Manager
 * @author hoang.pt
 */
public class StudentManager {

	/**
	 * use for read data not Scanner
	 */
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);
    
    ArrayList<Student> list = new ArrayList<Student>(1);

    Student student;

    /**
     * Input data
     * @author hoang.pt
     */
    void inputData() {

        try {
            System.out.println("Enter information:");
            System.out.println("roll number:");
            String rollNumber = in.readLine();
            System.out.println("name:");
            String name = in.readLine();
            System.out.println("address:");
            String address = in.readLine();
            System.out.println("age:");
            int age = Integer.parseInt(in.readLine());

            student = new Student(rollNumber, name, address, age);

        } catch (IOException ex) {
        }
    }

    /**
     * add to list
     * @author hoang.pt
     */
    void addtoList() {

        //loop for 5 student
        for (int i = 0; i < 2; i++) {
            inputData();
            list.add(student);
        }

        //print list
        //for (Student x : list) {
        //    System.out.print(x.getName() + " ");
        //}
    }

    /**
     * save to File
     * @author hoang.pt
     */
    void saveListtoFile() {

        //init writer
        FileOutputStream fileWriter = null;
        ObjectOutputStream writer = null;

        try {
            fileWriter = new FileOutputStream("student.txt");
            writer = new ObjectOutputStream(fileWriter);

            writer.writeObject(list);

            //close
            writer.flush();
            writer.close();
            fileWriter.close();

        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }

    /**
     * read file and display
     * @author hoang.pt
     */
    void readFiletoList() {
    	
    	FileInputStream fileReader = null;
    	ObjectInputStream reader = null;
    	
    	try {
			fileReader = new FileInputStream("student.txt");
			reader = new ObjectInputStream(fileReader);
			
			ArrayList<Student> list = (ArrayList<Student>) reader.readObject();
			
			for (Student student : list) {
				System.out.println(student);
			}
			
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} catch (ClassNotFoundException e) {
		}
    }
}
